package bookstudytest.stream.CollectionStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Auther: Lee
 * @Date: 2018/6/7 11:50
 * @Description:
 */
public class CollectionStream {
    static List<String> list = new ArrayList<>();

    public static void main(String[] args) {

        CollectionStream instance = new CollectionStream();
        instance.reduce("");

    }

    public void demoFilter() {
        listAddValue(list);
        //其中filter中传入的函数是一个判断句，其实就是predicate
        list.stream().filter(s -> s.startsWith("b")).forEach(System.out::println);
        System.out.println("````````````````````````````");
        list.forEach(System.out::println);
        list.stream().forEach(System.out::println);
        list.parallelStream().forEach(System.out::println);
    }

    public void demoSorted() {
        listAddValue(list);
        //sorted返回一个排过序的stream视图，而本身的集合stream并没有排序，排序规则是自然排序
        list.parallelStream().sorted().forEach(System.out::println);
        list.parallelStream().sorted(String::compareTo).forEach(System.out::println);
    }

    public void mapDemo() {
        listAddValue(list);
        list.stream().map((String o) -> Integer.valueOf(o.substring(3))).forEach(System.out::println);
    }

    public Boolean match() {
        listAddValue(list);
        list.stream().anyMatch(o -> o.startsWith("a"));
        list.stream().noneMatch(o -> o.startsWith("a"));
        return list.stream().allMatch(o -> o.startsWith("a"));
    }

    public void reduce(String star) {
        listAddValue(list);
        System.out.println(list.stream().reduce("head:", (o1, o2) -> o1 + o2));
    }

    public void listAddValue(List<String> list) {
        list.add("ddd2");
        list.add("aaa2");
        list.add("bbb1");
        list.add("aaa1");
        list.add("bbb3");
        list.add("ccc3");
        list.add("bbb2");
        list.add("ddd1");

    }

}
